package com.lesama.stream;

// 统计文件中字符出现的次数

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

public class CharacterTest {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("D:\\b.txt");
        HashMap<Character, Integer> map = new HashMap<>();
        int i;
        while((i = fr.read()) != -1) {
            char character = (char) i;
            if(map.containsKey(character)) {
                map.put(character, map.get(character) + 1);
            } else {
                map.put(character, 1);
            }
        }

        StringBuilder sb = new StringBuilder();
        map.forEach((c, idx) -> {
            sb.append(c).append("(").append(idx).append(")");
        });
        System.out.println(sb.toString());
    }
}